//
//  ApprovaledViewController.m
//  ZJControlsClass
//
//  Created by apple on 2024/10/25.
//

#import "ApprovaledViewController.h"
#import "PendApprovalInfoViewController.h"
#import "ZJPendApprovallNetWorking.h"
#import "ZJPendApprovallToDoListModel.h"
#import "publicNodataView.h"

#import "ApproveTableViewCell.h"



@interface ApprovaledViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSInteger pageNo;
    NSInteger pageSize;
}

@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSMutableArray * dataSource;
@property (nonatomic,strong) ZJPendApprovallDoListNetWorking * manager;
@property (nonatomic,strong) ZJPendApprovallToDoListModel * model;
@property (nonatomic,strong) NSMutableDictionary * paramer;

@property (nonatomic,strong) publicNodataView * nodataView;


@end

@implementation ApprovaledViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    pageNo = 1;
    pageSize = 20;
    [self networking];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = COLOR_RGB(244, 244, 244);
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.nodataView];

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.view);
    }];
    [self.nodataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.view);
    }];
    
    
}

-(void)networking{
    
    
    [self.paramer setValue:[NSString stringWithFormat:@"%@",@(pageNo)] forKey:@"pageNo"];
    [self.paramer setValue:[NSString stringWithFormat:@"%@",@(pageSize)] forKey:@"pageSize"];
    
    [self.manager dispatchTaskWithParam:self.paramer completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (responseObject && [responseObject[@"code"] integerValue] == 200) {
                
            if (self->pageNo == 1) {
                [self.dataSource removeAllObjects];
            }
            NSArray * data = responseObject[@"data"][@"list"];
            
            if (data.count == 0) {
                self.nodataView.hidden = NO;
            }else{
                self.nodataView.hidden = YES;
            }
            
            if (data.count < 20)[self.tableView.mj_footer endRefreshingWithNoMoreData];
            else [self.tableView.mj_footer endRefreshing];
            [self.dataSource  addObjectsFromArray:[ZJPendApprovallToDoListModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]]];
               
            [self.tableView reloadData];
            
        }else{
            [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:^{
                
            }];
        }
        
        [self.tableView.mj_header endRefreshing];
        
        
    }];
    
    
    
}





#pragma mark -- <UITableViewDelegate,UITableViewDataSource>
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ApproveTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ApproveTableViewCell"];
    if (!cell) {
        cell = [[ApproveTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"ApproveTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    ZJPendApprovallToDoListModel * model = self.dataSource[indexPath.row];
    cell.model = model;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ALIGN_HEIGHT_PT(175);
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PendApprovalInfoViewController * vc = [PendApprovalInfoViewController new];
    ZJPendApprovallToDoListModel * model = self.dataSource[indexPath.row];
    vc.taskId = model.taskId;
    vc.processInstanceId = model.processInstanceId;
    vc.titleStr = model.processDefinitionName;
    vc.personStr = model.startUser.name;
    vc.timeStr = model.startTime;
    vc.showOrHidden = YES;
    
    
    [self.navigationController pushViewController:vc animated:YES];
}




#pragma mark -- 懒加载
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = COLOR_RGB(243, 243, 243);
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            pageNo = 1;
            [self.paramer addEntriesFromDictionary:@{@"pageNo":@(pageNo)}];
            [self networking];
        }];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            pageNo += 1;
            [self.paramer addEntriesFromDictionary:@{@"pageNo":@(pageNo)}];
            [self networking];
        }];
        
        
        
    }
    return _tableView;
}

-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

-(ZJPendApprovallDoListNetWorking *)manager{
    if (!_manager) {
        _manager = [ZJPendApprovallDoListNetWorking new];
    }
    return _manager;
}
-(NSMutableDictionary *)paramer{
    if (!_paramer) {
        _paramer = [NSMutableDictionary dictionary];
    }
    return _paramer;
}

-(publicNodataView *)nodataView{
    if (!_nodataView) {
        _nodataView = [publicNodataView new];
        _nodataView.hidden = YES;
    }
    return _nodataView;
}


@end
